package com.cristina.attention.rest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cristina.attention.entity.User;
import com.cristina.attention.mp.support.Condition;
import com.cristina.attention.service.UserService;
import com.cristina.attention.utils.Func;
import com.cristina.attention.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * @Author superman
 * @Date 2021/11/19 20:59
 **/
@Slf4j
@ApiIgnore
@RestController
@RequestMapping("core/user")
@AllArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 查询单条
     */
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    public R<Object> detail(User user) {
        User detail = userService.getOne(Condition.getQueryWrapper(user));
        return R.data(detail);
    }

    /**
     * 查询单条
     */
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/info")
    public R<Object> info(User user) {
        User detail = userService.getById(user.getUserId());
        return R.data(detail);
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R<Object> list() {
        return R.data(userService.list());
    }

    @PatchMapping("/submit")
    @ApiOperation("新增或修改")
    public R<Object> submit(@Valid @RequestBody User user) {
        return R.status(userService.submit(user));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "传入User")
    public R<Object> update(@Valid @RequestBody User user) {
        return R.status(userService.updateById(user));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@RequestParam String ids) {
        return R.status(userService.deleteLogic(Func.toLongList(ids)));
    }

    @PostMapping("/update_password")
    @ApiOperation(value = "修改密码", notes = "传入密码")
    public R<Object> updatePassword() {
        return R.status(true);
    }

    private final KafkaTemplate<String, Object> kafkaTemplate;

    @PostMapping("/message/send")
    public void send(@RequestBody User user) {
        log.info("准备发送消息为：{}", user);
        //发送消息
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send("attention", JSON.toJSONString(user));
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onFailure(Throwable throwable) {
                //发送失败的处理
                log.info("attention" + " - 生产者 发送消息失败：" + throwable.getMessage());
            }

            @Override
            public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                //成功的处理
                log.info("attention" + " - 生产者 发送消息成功：" + stringObjectSendResult.toString());
            }
        });
    }

    @GetMapping("/findUserByUsername")
    @ApiOperation(value = "详情", notes = "传入username")
    public R<Object> findUserByUsername(@RequestParam String username) {
        return R.data(userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername, username)));
    }

}
